﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using NHibernate;
using ZLCMS.Business;
using ZLCMS.Data.Model;
using ZLCMS.Business.DwzFrame;

namespace ZLCMS.Web.SystemManage.Basics.RightConfig.Right
{
    public partial class RightDetail : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                int uid = 0;
                if (Int32.TryParse(Request["uid"], out uid))
                {
                    using (ISession session = DotNet.Common.NHibernateUtil.SessionFactoryHelper.CreateSession())
                    {
                        UrRight right = session.Get<UrRight>(uid);
                        if (right != null)
                        {
                            name.Text = right.RightName;
                            code.Text = right.RightCode;
                            group.Text = right.GroupCode;
                            ViewState["uid"] = uid;
                        }
                    }
                }
                if (string.IsNullOrEmpty(group.Text))
                {
                    group.Text = Request["group"];
                }
            }
            else
            {
                using (ISession session = DotNet.Common.NHibernateUtil.SessionFactoryHelper.CreateSession())
                {
                    UrRight right = null;
                    if (ViewState["uid"] != null)
                    {
                        right = session.Get<UrRight>(ViewState["uid"]);
                    }
                    if (right == null)
                    {
                        right = new UrRight();
                    }
                    right.GroupCode = group.Text;
                    right.RightName = name.Text;
                    right.RightCode = code.Text;
                    session.SaveOrUpdate(right);
                    session.Flush();
                    ViewState["uid"] = right.Id;
                    FrameHelper _framePage = new FrameHelper(this);
                    ReturnCode rc = new ReturnCode()
                    {
                        statusCode = 200,
                        message = "操作成功",
                        callbackType = "forward",
                        forwardUrl = "/SystemManage/Basics/RightConfig/Right/RightDetail.aspx?group=" + group.Text
                    };
                    if (Request["submit_op"] == "0")
                    {
                        rc.forwardUrl += "&uid=" + ViewState["uid"] ;
                    }
                    _framePage.Alert(rc);
                }
            }
        }
    }
}
